package com.weibo.sina;

import weibo4android.User;
import weibo4android.Weibo;
import weibo4android.WeiboException;
import weibo4android.androidexamples.OAuthConstant;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.DateFormat;
import android.widget.ImageView;
import android.widget.TextView;

import com.weibo.util.MD5;
import com.weibo.util.Util;

public class Profile extends Activity {
	private User user;
	
	private final static int UPDATEFACE = 0x00;
	private final Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
			case UPDATEFACE:
				ImageView face = (ImageView)findViewById(R.id.face);
				face.setImageDrawable((Drawable)msg.obj);
				break;
			}
			super.handleMessage(msg);
		}
		
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.profile);
		Weibo weibo = OAuthConstant.getInstance().getWeibo();
		try {
			user = weibo.verifyCredentials();
		} catch (WeiboException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ImageView face = (ImageView)findViewById(R.id.face);
		String key = MD5.getMD5(user.getProfileImageURL().toString()
				.getBytes());
		Drawable imageDrawable = Util.loadImage(key, user.getProfileImageURL()
				.toString(),getBaseContext(),mHandler,UPDATEFACE);
		if (imageDrawable != null)
			face.setBackgroundDrawable(imageDrawable);
		else {
			face.setBackgroundResource(R.drawable.face);
		}
		TextView textView = (TextView)findViewById(R.id.nice_name);
		textView.setText(user.getName());
		textView = (TextView)findViewById(R.id.create_time);
		String today=DateFormat.format(getBaseContext().getString(R.string.full_wday_month_day_no_year), user.getCreatedAt()).toString();
		textView.setText(today);
		
		textView = (TextView)findViewById(R.id.fance_count);
		String count = getResources().getString(R.string.fance_count);
		count = String.format(count, user.getFriendsCount());
		textView.setText(count);
		
		textView = (TextView)findViewById(R.id.follow_count);
		count = getResources().getString(R.string.follow_count);
		count = String.format(count, user.getFollowersCount());
		textView.setText(count);
		
		textView = (TextView)findViewById(R.id.favourites_count);
		count = getResources().getString(R.string.favourites_count);
		count = String.format(count, user.getFavouritesCount());
		textView.setText(count);
		
		textView = (TextView)findViewById(R.id.description);
		textView.setText(user.getDescription());
		
		textView = (TextView)findViewById(R.id.address);
		textView.setText(user.getLocation());
		
		textView = (TextView)findViewById(R.id.blog_url);
		if(user.getURL()!=null)
		textView.setText(user.getURL().toString());
		super.onCreate(savedInstanceState);
	}
}
